package utilities;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * Combinations
 * @author Zbyszko
 */
public class Combinations {

    private double array[];
    private int n;

    public Combinations(double[] array, int n) {
        this.array = array;
        this.n = n;
    }

    public List<double[]> getVariations() {

        int permutations = (int) Math.pow(array.length, n);
        double[][] table = new double[permutations][n];

        for (int x = 0; x < n; x++) {
            int t2 = (int) Math.pow(array.length, x);
            for (int p1 = 0; p1 < permutations;) {
                for (int al = 0; al < array.length; al++) {
                    for (int p2 = 0; p2 < t2; p2++) {
                        table[p1][x] = array[al];
                        p1++;
                    }
                }
            }
        }

        List<double[]> result = new ArrayList<>();
        for (double[] permutation : table) {
            result.add(permutation);
        }

        return result;
    }

    public static void runExample() {

        Combinations gen = new Combinations(
                new double[] { 0, 0.5, 1 }, 
                3
        );
        List<double[]> v = gen.getVariations();
        for (double[] s : v) {
            System.out.println(Arrays.toString(s));
        }
        System.out.println("Combinations: " + v.size());
    }

}
